//
//  ViewController.m
//  音频播放测试
//
//  Created by dllo on 16/7/29.
//  Copyright © 2016年 YMJ. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "YMJlrc.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) AVAudioPlayer *player;
@property (nonatomic, strong) YMJlrc *lrc;
@property (nonatomic, strong) UITableView *tableVIew;
@property (nonatomic, assign) NSInteger currentRow;
@property (nonatomic, strong) UISlider *slider;
@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, strong) NSMutableArray *songArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 背景设置
    [self initBackground];
    // 初始化播放器
    [self initPlayer];
    // 创建视图
    [self createView];
    // 监听当前时间
    self.count = 0;
    self.songArray = [NSMutableArray array];
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
    self.lrc = [[YMJlrc alloc] init];
    [_lrc papareLyric];

}
- (void)updateTime {
    self.currentRow = 0;
    CGFloat currentTime = _player.currentTime;
    self.label1.text = [NSString stringWithFormat:@"%02d:%02d",(int)currentTime / 60,(int)currentTime % 60];
    self.slider.value = currentTime / _player.duration;
    for (int i = 0; i < _lrc.timeArr.count; i++) {
        NSArray *arr = [_lrc.timeArr[i] componentsSeparatedByString:@":"];
        CGFloat lyricTime = [arr[0] floatValue] * 60 + [arr[1] floatValue];
        if (_player.currentTime > lyricTime) {
            _currentRow = i;
        } else {
            break;
        }
    }
    [self.tableVIew reloadData];
    [self.tableVIew scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_currentRow inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
- (void)createView {
    self.tableVIew = [[UITableView alloc] initWithFrame:CGRectMake(0, 200, 414, 736 / 3) style:UITableViewStylePlain];
    [self.view addSubview:_tableVIew];
    self.tableVIew.backgroundColor = [UIColor clearColor];
    _tableVIew.delegate = self;
    _tableVIew.dataSource = self;
    // 去掉分割线
    _tableVIew.separatorStyle = UITableViewCellSelectionStyleNone;
    [_tableVIew registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
    
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(7, 462, 401, 31)];
    [self.view addSubview:_slider];
    self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(9, 500, 51, 21)];
    [self.view addSubview:_label1];
    self.label2 = [[UILabel alloc] initWithFrame:CGRectMake(355, 500, 51, 21)];
    [self.view addSubview:_label2];
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(193, 542, 48, 32);
    [self.view addSubview:_button];
    _button.layer.masksToBounds = YES;
    [_button setTitle:@"播放" forState:0];
    [_button setTitle:@"暂停" forState:UIControlStateSelected];
    [_button addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftButton.frame = CGRectMake(9, 542, 56, 33);
    [self.view addSubview:_leftButton];
    [_leftButton setTitle:@"上一首" forState:0];
//    [_leftButton setTitle:@"暂停" forState:UIControlStateSelected];
    [_leftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.frame = CGRectMake(355, 540, 56, 33);
    [self.view addSubview:_rightButton];
    [_rightButton setTitle:@"下一首" forState:0];
    //    [_leftButton setTitle:@"暂停" forState:UIControlStateSelected];
    [_rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _lrc.timeArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    if (indexPath.row == _currentRow) {
        cell.textLabel.textColor = [UIColor colorWithRed:0.231 green:0.651 blue:0.376 alpha:1.000];
    } else {
        cell.textLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.3];
    }
    cell.textLabel.textAlignment = 1;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.text = _lrc.lyricArr[indexPath.row];
    return cell;
}
- (void)initPlayer {
    // 后台播放
//    AVAudioSession *session = [AVAudioSession sharedInstance];
//    [session setActive:YES error:nil];
//    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    // 接受远程控制事件
//    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"世界上最难唱的歌" ofType:@"mp3"];
    NSURL *url = [NSURL fileURLWithPath:path];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    // 声道 (-1)左声道 右声道(1)
//    self.player.pan = 0;
    // 音量
//    self.player.volume = 1;
    // 单曲循环(负数表示单曲循环)
    self.player.numberOfLoops = -1;
    // 速率(默认为1)
    //    self.player.enableRate = YES;
    //    self.player.rate = 1.0;
    // 总时间
    CGFloat totalSeconds = _player.duration;
    // 播放
    //    [self.player play];
    self.label2.text = [NSString stringWithFormat:@"%02d:%02d",(int)totalSeconds / 60,(int)totalSeconds % 60];

}
- (void)rightAction {
    
    [_tableVIew reloadData];
}
- (void)playAction:(UIButton *)button {
    button.selected = !button.selected;
    if (button.selected) {
        [_player prepareToPlay];
        [_player play];
    } else {
        [_player pause];
    }
}
- (void)initBackground {
    self.view.backgroundColor = [UIColor clearColor];
    // 建立个毛玻璃
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:2];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    effectView.frame = CGRectMake(0, 200, 414, 736 / 3);
    [self.view addSubview:effectView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
